Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service-worker): handle offline mode and ignore invalid only-if-cached requests #22883

Closed
wants to merge 4 commits into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Mar 20, 2018

PR Checklist

PR Type

[x] Bugfix

What is the current behavior?

This PR fixes two issues:

  1. When trying to fetch ngsw.json (e.g. during checkForUpdate()) while either the client or the server are offline, the ServiceWorker would enter a degrade mode, where only existing clients would be served. This essentially means that the ServiceWorker doesn't work offline.
  2. Under some circumstances (possibly related to opening Chrome DevTools), requests are made with cache: 'only-if-cached' and mode: 'no-cors'. These request will eventually fail, because only-if-cached is only allowed to be used with mode: 'same-origin'.
    This is likely a bug in Chrome DevTools. See also Failed to load resource: the server responded with a status of 504 (Gateway Timeout) #22362 (comment).

Issue Number: #21636, #22362

What is the new behavior?

  1. Offline errors (status: 504) are treated differently than other fetch errors and the SW doesn't enter degraded mode. The ServiceWorker will remain in the current mode until connectivity to the server is restored.
  2. Invalid only-if-cached requests (i.e. with mode !== 'same-origin') are not handled by the SW.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Fixes #21636 and #22362.

@gkalpak gkalpak requested a review from alxhub March 20, 2018 13:25
@gkalpak gkalpak added type: bug/fix action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release area: service-worker Issues related to the @angular/service-worker package labels Mar 20, 2018
@mary-poppins
Copy link

You can preview aa7b71c at https://pr22883-aa7b71c.ngbuilds.io/.

// (See also https://github.com/angular/angular/issues/22362.)
// TODO(gkalpak): Remove once no longer necessary (i.e. fixed in Chrome DevTools).
if ((req.cache as string) === 'only-if-cached' && req.mode !== 'same-origin') {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's console.error the first time this happens so it's not a silent failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a failure? It is just that the SW won't handle this request, so it will pass through (and I assume the browser will do the right thing).
(Interestingly, no error is logged when the SW doesn't handle such requests.)

Copy link
Member

@alxhub alxhub Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if someone is looking in DevTools, expecting to see their request answered (from ServiceWorker), that having a message locally there saying "hey, the SW didn't handle this request because DevTools sent it in an invalid way" is beneficial.

I'm okay if we only log the first time it happens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed "offline", this is not a request made by the app/user, but a background request made by DevTools (apparently). So, we'll debugger.log() the first time we see it.

I have pushed another commit. PTAL

@mary-poppins
Copy link

You can preview 0535086 at https://pr22883-0535086.ngbuilds.io/.

Previously, when trying to fetch `ngsw.json` (e.g. during
`checkForUpdate()`) while either the client or the server were offline,
the ServiceWorker would enter a degrade mode, where only existing
clients would be served. This essentially meant that the ServiceWorker
didn't work offline.
This commit fixes it by differentiating offline errors and not entering
degraded mode. The ServiceWorker will remain in the current mode until
connectivity to the server is restored.

Fixes angular#21636
Under some circumstances (possibly related to opening Chrome DevTools),
requests are made with `cache: 'only-if-cached'` and `mode: 'no-cors'`.
These request will eventually fail, because `only-if-cached` is only
allowed to be used with `mode: 'same-origin'`.
This is likely a bug in Chrome DevTools.

This commit avoids errors related to such requests by not handling them.

Fixes angular#22362
@mary-poppins
Copy link

You can preview 5fae6e7 at https://pr22883-5fae6e7.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 0235d68 at https://pr22883-0235d68.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 76109cb at https://pr22883-76109cb.ngbuilds.io/.

@gkalpak gkalpak added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 27, 2018
@Aarchana
Copy link

Aarchana commented Mar 27, 2018

Hi, when can we expect this to be merged so that we can start playing with the service worker offline mode capability:)

@gkalpak
Copy link
Member Author

gkalpak commented Mar 27, 2018

It is marked for merge, so I guess soon 😃

@alxhub alxhub closed this in 12665a7 Mar 28, 2018
alxhub pushed a commit that referenced this pull request Mar 28, 2018
Previously, when trying to fetch `ngsw.json` (e.g. during
`checkForUpdate()`) while either the client or the server were offline,
the ServiceWorker would enter a degrade mode, where only existing
clients would be served. This essentially meant that the ServiceWorker
didn't work offline.
This commit fixes it by differentiating offline errors and not entering
degraded mode. The ServiceWorker will remain in the current mode until
connectivity to the server is restored.

Fixes #21636

PR Close #22883
alxhub pushed a commit that referenced this pull request Mar 28, 2018
Under some circumstances (possibly related to opening Chrome DevTools),
requests are made with `cache: 'only-if-cached'` and `mode: 'no-cors'`.
These request will eventually fail, because `only-if-cached` is only
allowed to be used with `mode: 'same-origin'`.
This is likely a bug in Chrome DevTools.

This commit avoids errors related to such requests by not handling them.

Fixes #22362

PR Close #22883
alxhub pushed a commit that referenced this pull request Mar 28, 2018
Previously, when trying to fetch `ngsw.json` (e.g. during
`checkForUpdate()`) while either the client or the server were offline,
the ServiceWorker would enter a degrade mode, where only existing
clients would be served. This essentially meant that the ServiceWorker
didn't work offline.
This commit fixes it by differentiating offline errors and not entering
degraded mode. The ServiceWorker will remain in the current mode until
connectivity to the server is restored.

Fixes #21636

PR Close #22883
alxhub pushed a commit that referenced this pull request Mar 28, 2018
Under some circumstances (possibly related to opening Chrome DevTools),
requests are made with `cache: 'only-if-cached'` and `mode: 'no-cors'`.
These request will eventually fail, because `only-if-cached` is only
allowed to be used with `mode: 'same-origin'`.
This is likely a bug in Chrome DevTools.

This commit avoids errors related to such requests by not handling them.

Fixes #22362

PR Close #22883
@gkalpak gkalpak deleted the fix-sw-offline branch March 28, 2018 18:34
@Aarchana
Copy link

Hi i created a new angular cli project with the service worker enabled , still i see when app is made offline the 504 error appears. Can anyone suggest on this please:)

@gkalpak
Copy link
Member Author

gkalpak commented Apr 2, 2018

The fix is included in @angular/service-worker@6.0.0-rc.1. Make sure you are using that version for all packages.

If you don't want to use a version that is not yet marked as stable, you need to wait a bit for the final 6.0 release (currently scheduled for this week).

@Aarchana
Copy link

Aarchana commented Apr 5, 2018

@gkalpak ,
Thanks a lot.

@tobyStaff
Copy link

can anyone tell me when #22883 is likely to go into a release?
A quick search of the 5.2.10 -> 6 tells me that it hasn't been included yet.

@probert94
Copy link

As much as I remember, it is already in version 5.2.10.
But you should take a look at the changelog

@gkalpak
Copy link
Member Author

gkalpak commented May 5, 2018

@Springrbua is right. It is in 5.2.10 and 6.x.

@ALGDB
Copy link

ALGDB commented May 8, 2018

I have the same problem. I've updated to v6.x

"dependencies": {
    "@angular/animations": "6.0.0",
    "@angular/cdk": "^6.0.0",
    "@angular/common": "6.0.0",
    "@angular/compiler": "6.0.0",
    "@angular/core": "6.0.0",
    "@angular/flex-layout": "^6.0.0-beta.15",
    "@angular/forms": "6.0.0",
    "@angular/http": "6.0.0",
    "@angular/material": "6.0.1",
    "@angular/platform-browser": "6.0.0",
    "@angular/platform-browser-dynamic": "6.0.0",
    "@angular/router": "6.0.0",
    "@angular/service-worker": "6.0.0",

that's the error on Chrome 66.0.3359.139

e {headers: t, status: 504, statusText: "Gateway Timeout", url: "http://localhost:3000/api/v1/externalHumPre", ok: false, …}
error:null
headers:t {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message:"Http failure response for http://localhost:3000/api/v1/externalHumPre: 504 Gateway Timeout"
name:"HttpErrorResponse"
ok:false
status:504
statusText:"Gateway Timeout"
url:"http://localhost:3000/api/v1/externalHumPre"

@gkalpak
Copy link
Member Author

gkalpak commented May 9, 2018

Could be a different problem. Please open a new issue and make sure you provide a minimal reproduction.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: service-worker Issues related to the @angular/service-worker package cla: yes target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ServiceWorker] localhost took too long to respond. HTTP ERROR 504
8 participants